#ifndef KUN_RANDER_PIPELINE_TOON_LIT_FORWARD_PASS_INCLUDED
#define KUN_RANDER_PIPELINE_TOON_LIT_FORWARD_PASS_INCLUDED

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"

struct Attributes
{
    float3 positionOS       : POSITION;
    float3 normalOS          : NORMAL;
    float4 tangentOS         : TANGENT;
    float2 uv               : TEXCOORD0;
    UNITY_VERTEX_INPUT_INSTANCE_ID
};

struct Varyings
{
    float4 positionCS               : SV_POSITION;
    float2 uv                       : TEXCOORD0;
    float3 positionWS               : TEXCOORD1;
    float3 normalWS                 : TEXCOORD2;
    UNITY_VERTEX_INPUT_INSTANCE_ID
    UNITY_VERTEX_OUTPUT_STEREO
};


sampler2D _MainTex; 
sampler2D _ILMTex;

CBUFFER_START(UnityPerMaterial)
    // base color
    float4 _MainTex_ST;
    float4 _MainColor;
    float _ILMTex_ST;

    // shadow
    float4 _ShadowColor;
    float _ShadowRange;
    float _ShadowSmooth;

    // specular
    float _SpecularRange;
    float _SpecularMulti;
    float _SpecularGloss;

    // rim
    float4 _RimColor;
    float _RimMin;
    float _RimMax;
    float _RimSmooth;

CBUFFER_END


Varyings ToonLitForwardPassVertex(Attributes input)
{
    Varyings output = (Varyings)0;

    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_TRANSFER_INSTANCE_ID(input, output);
    UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);

    VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
    VertexNormalInputs vertexNormalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
    output.positionCS = vertexInput.positionCS;
    output.positionWS = vertexInput.positionWS;
    output.normalWS = vertexNormalInput.normalWS;
    output.uv = TRANSFORM_TEX(input.uv, _MainTex);;

    return output;
}

float4 ToonLitForwardPassFragment(Varyings input) : SV_Target
{
    UNITY_SETUP_INSTANCE_ID(input);
    UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);

    float4 color = float4(1, 1, 1, 1);

    float3 positionWS = input.positionWS;
    float3 normalWS = normalize(input.normalWS);
    float3 viewDirectionWS = normalize(GetCameraPositionWS() - positionWS);
    
    Light mainLight = GetMainLight();
    float3 lightDirectionWS = mainLight.direction;

    float4 mainTex = tex2D(_MainTex, input.uv);
    float4 ilmTex = tex2D(_ILMTex, input.uv);

    float3 diffuse = 0;
    float halfLambert = dot(normalWS, lightDirectionWS) * 0.5 + 0.5;
    // float threshold = (halfLambert + ilmTex.g) * 0.5;
    // float ramp = saturate(_ShadowRange  - threshold);
    // ramp = smoothstep(0, _ShadowSmooth, ramp);
	// diffuse = lerp(_MainColor, _ShadowColor, ramp);
	diffuse = mainTex.rgb;

    if (halfLambert <= (1 - ilmTex.g))
    {
        diffuse *= _ShadowColor; 
    }

    float3 specular = 0;
	// float3 halfDirection = normalize(lightDirectionWS + viewDirectionWS);
	// float specularSize = pow(max(0, dot(normalWS, halfDirection)), _SpecularGloss);
	// float specularMask = ilmTex.b;
	// if (specularSize >= 1 - specularMask * _SpecularRange)
	// {
	// 	specular = _SpecularMulti * (ilmTex.r);
	// }

    float3 rimColor = 0;

    #ifdef _RIM_ENABLE_ON
        float f = 1.0 - saturate(dot(viewDirectionWS, normalWS));
        float rim = smoothstep(_RimMin, _RimMax, f);
        rim = smoothstep(0, _RimSmooth, rim);
        rimColor = rim * _RimColor.rgb * _RimColor.a;
    #endif

    color.rgb = (diffuse + specular + rimColor);

    float temp = float4(rimColor, 1);
    return color;
}


#endif